package com.algorithm.code.stack;


import java.util.Arrays;

/**
 * @description TODO
 * @program: sudy
 * @author: kukuxiahuni
 * @create: 2020-01-25 15:41
 **/
public class ArrayStack<T> implements BaseStack<T> {

    /**
     * 栈空间容量
     */
    private int capaticy = 10;


    /**
     * 栈指针
     */
    private int index = -1;

    /**
     * 栈数据
     */
    private final T[] data;


    public static void main(String[] args) {
        ArrayStack<Integer> arrayStack = new ArrayStack<>();
        for (int i = 0; i < 20; ++i) {
            arrayStack.push(i);
        }

        System.out.println(arrayStack.peek());
        System.out.println(arrayStack.pop());
        arrayStack.push(20);
        System.out.println(arrayStack.peek());
    }

    /**
     * 构造函数
     */
    public ArrayStack() {
        this.data = (T[]) new Object[capaticy];
    }

    public ArrayStack(int capaticy) {
        this.capaticy = capaticy;
        this.data = (T[]) new Object[capaticy];
    }

    @Override
    public boolean isEmpty() {
        return this.data.length == 0;
    }

    @Override
    public T pop() {
        if (isEmpty()) {
            return null;
        }

        T val = this.data[index];
        this.data[index] = null;
        index--;
        return val;
    }

    @Override
    public T peek() {
        if (isEmpty()) {
            return null;
        }
        return this.data[index];
    }

    @Override
    public void push(T value) {
        if (value == null) {
            return;
        }

        if (this.index >= this.data.length - 1) {
            // 栈空间满，无法插入
            return;
        }

        this.data[++index] = value;

    }
}
